-- boss
-- created by panyl
-- 击杀boss

-- 怪物死亡事件
EventMgr.register("gift_boss", event.BOSS_DIE, function(para)
    local gem = para["gem"];

    local config = GiftM.query("boss");

    local progress = ME.user.dbase:query("gift_progress", {});
    local stat     = progress["boss"] or {};

    local dungeonId = DungeonM.getDungeonId();
    local layer     = DungeonM.currentLayer();
    for grade, info in pairs(config) do
        local require = info["require"];
        if require["dungeon_id"] == dungeonId and
            require["layer"] == layer then
            stat[grade] = (stat[grade] or 0) + 1;
        end
    end

    progress["boss"] = stat;

    ME.user.dbase:set("gift_progress", progress);
end);

return {
    getProgress = function()
        -- 已消费的钻石
        local progress = ME.user.dbase:query("gift_progress", {});

        return progress["boss"];
    end,

    checkProgress = function(grade)
        -- 已消费的钻石
        local progress = ME.user.dbase:query("gift_progress", {});
        local stat = progress["boss"] or {};

        local config = GiftM.query("boss", grade);
        local require = config["require"];
        local times = require["times"] or 1;

        return times <= (stat[grade] or 0);
    end,

    hasTook = function(grade)
        local received = ME.user.dbase:query("received_gift", {});
        local record = received["boss"] or 0;

        local b = bit.blshift(1, grade);
        if bit.band(record, b) ~= 0 then
            -- 已经领取过了
            return true;
        end

        return false;
    end,

    recordTook = function(grade)
        local received = ME.user.dbase:query("received_gift", {});
        local record = received["boss"] or 0;

        -- 标记
        received["boss"] = record + bit.blshift(1, grade);

        ME.user.dbase:set("received_gift", received);
    end,
};
